<!--
 * @Descripttion: HN-SaaS管理系统
 * @version: 1.0.0
 * @Author: 侯建军
 * @Date: 2020-09-04 10:26:00
 * @LastEditors: 侯建军
 * @LastEditTime: 2020-09-05 14:26:19
-->
<template>
  <div>
    <div class="addRole">
      <el-button type="primary" @click="addrole">添加角色</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="roleData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" prop="id"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="创建日期" prop="createDate"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button size="mini">角色用户</el-button>
          <el-button size="mini">角色授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form :model="role" :rules="rules" ref="roleForm" label-width="100px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="role.roleName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { insert, update, remove, getRoles } from "@/api/role.js";

export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      title: "添加角色",
      role: {},
      roleData: [],
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空！", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 10 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 加载数据
    this.loadData();
  },
  methods: {
    loadData() {
      getRoles().then((res) => {
        if (res.data.code === "0000") {
          this.roleData = res.data.data;
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      this.role = row;
      this.dialogVisible = true;
      this.title = "修改角色";
    },
    handleDelete(index, row) {
      this.$confirm("确认要删除该角色吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(row.id);
          remove(row.id).then((res) => {
            if (res.data.code == "0000") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });

              this.loadData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    addrole() {
      this.dialogVisible = true;
      this.title = "添加角色";
    },
    save() {
      this.$refs["roleForm"].validate((valid) => {
        if (valid) {
          if (this.title === "添加角色") {
            this.addUserSave();
          } else if (this.title === "修改角色") {
            this.editUserSave();
          }
         
        } else {
          return false;
        }
      });
    },
    addUserSave() {
      insert(this.role).then((res) => {
        if (res.data.code == "0000") {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.dialogVisible = false;
          this.loadData();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    editUserSave() {
      update(this.role).then((res) => {
        if (res.data.code == "0000") {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.dialogVisible = false;
          this.loadData();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.addRole {
  text-align: left;
  margin-bottom: 10px;
}
</style>